﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

namespace DevelopTools.UI
{
    /// <summary>
    /// 打字效果
    /// </summary>
    public class UIAnimation_TypeWritter : UIAnimation
    {
        //获取UItext
        private UIText thisText;
        /// <summary>
        /// 字符串：需要打印效果
        /// </summary>
        public string texts;
        /// <summary>
        /// 打字中的时间变量
        /// </summary>
        private float timer;
        /// <summary>
        /// 每秒中打印字符个数
        /// </summary>
        public int charsPerSecond = 1;
        /// <summary>
        /// 是否为首次打印
        /// </summary>
        private bool isNotFirst = false;
        void OnEnable()
        {
            Init();
        }

        public override void Init()
        {
            thisText = GetComponent<UIText>();
            thisText.text = "";
            timer = 0;
            isLoop = false;
            //Debug.Log("OnEnable  ");
        }

        // Use this for initialization
        public override void Start()
        {
            thisText = GetComponent<UIText>();
            //texts = thisText.text;
            thisText.text = "";
            isNotFirst = true;
            //Debug.Log("Start  ");
        }

        // Update is called once per frame
        public override void Update()
        {
            PlayAnimation();
        }
        public override void OnDestroy()
        {
            thisText = null;
            texts = "";
            timer = 0;
            isNotFirst = false;
            isLoop = false;
        }
        /// <summary>
        /// 播放打字动画
        /// </summary>
        public override void PlayAnimation()
        {
            if (texts.Length >= (int)(charsPerSecond * timer))
            {
                thisText.text = texts.Substring(0, (int)(charsPerSecond * timer));
                timer += Time.deltaTime;
            }
        }
    }
}